package com.miroservice.chapter2.web;


import com.miroservice.chapter2.common.HttpResponse;
import com.miroservice.chapter2.pojo.Singer;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class SingerController {



    /**
     * 添加@Valid 注解
     * @param singer
     * @return
     */

 /*   @PostMapping("/singer")
    private String validator(@Valid @RequestBody Singer singer) {
        System.out.print(singer);
        return "ok";
    }*/

   @PostMapping("/singer")
    private HttpResponse validator(@Valid @RequestBody Singer singer, BindingResult result) {
        if (result.hasErrors()) {
            return HttpResponse.error(result.getFieldError().getDefaultMessage());
        }
        return HttpResponse.ok();
    }
}
